﻿using Groep2TINGHService;
using Groep2TINGHService.Get_Set_Classes;
using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1.UserView
{
    /// <summary>
    /// Interaction logic for SamenSterkerUsers.xaml
    /// </summary>
    public partial class SamenSterkerUsers : MetroWindow
    {
        public SamenSterkerUsers()
        {
            InitializeComponent();
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();

            //Aanmaken tabblad
            Location location = new Location();
            List<Location> lijstLocation = myClient.GetAllLocations();
            lijstLocation.Add(location);
            foreach (Location r in lijstLocation)
            {
                lstLocation.Items.Add(r.Name);
            }

            //Wijzig/Verwijder tabblad
            Reservation reservation = new Reservation();
            List<Reservation> lijstReservation = myClient.GetAllReservations();
            lijstReservation.Add(reservation);
            foreach (Reservation r in lijstReservation)
            {
                lstReservations.Items.Add(r.Id);
            }

            //Calendar Overzicht highlighten
            foreach (Reservation r in lijstReservation)
            {
                DateTime d = r.StartDate;
                int year = d.Year;
                int month = d.Month;
                int day = d.Day;
                
                if (year > 2000)
                {
                    cldOverzicht.BlackoutDates.Add(new CalendarDateRange(new DateTime(year, month, day), new DateTime(year, month, day)));
                }

            }

            Contract contract = new Contract();
            List<Contract> LijstCompaniesStop = myClient.GetAllContracts();
            LijstCompaniesStop.Add(contract);
            foreach (Contract r in LijstCompaniesStop)
            {
                lstBedrijvenStopzetten.Items.Add(r.Id);
            }

        }


        private void btnBevestig_Click(object sender, RoutedEventArgs e)
        {
            if (Convert.ToDateTime(dtpDate.SelectedDate) < DateTime.Today)
            {
                MessageBox.Show("Incorrecte datum ingave");
            }
            else
            {
                ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
                Reservation reservation = new Reservation();

                //Opvragen van Locatie Id
                string locationName = lstLocation.SelectedItem.ToString();
                MessageBox.Show(locationName);
                Guid locationId = myClient.GetIdfromLocation(locationName);
                reservation.LocationId = locationId;

                reservation.StartDate = Convert.ToDateTime(dtpDate.SelectedDate);

                //Opvragen van Company Id

                /*
                Guid companyId = myClient.Get_CompanyByName(GEEN IDEE);
                reservation.CompanyId = companyId;
                reservation.CreateDate = DateTime.Today;
                 */

                if (myClient.InsertReservation(reservation))
                {
                    MessageBox.Show("Uw reservatie is succesvol gemaakt");
                }
                else
                {
                    MessageBox.Show("Uw reservatie is mislukt");
                }

            }

        }

        private void lstLocation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void btnWijzig_Click(object sender, RoutedEventArgs e)
        {
            Reservation reservation = new Reservation();
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            string res = lstReservations.SelectedItem.ToString();
            DateTime dat = Convert.ToDateTime(dtpDateWijzig.SelectedDate);
            if (myClient.ModifyReservation(dat, res))
            {
                MessageBox.Show("Wijziging van reservatie succesvol");
            }
            else
            {
                MessageBox.Show("Wijziging van reservatie mislukt");
            }
        }

        private void lstReservations_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            if (lstReservations.Items.Count > 0)
            {
                ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
                string res = lstReservations.SelectedItem.ToString();
                txtLocation.Text = myClient.GetNamefromLocation(res);
                Reservation reservation = new Reservation();
                dtpDateWijzig.SelectedDate = myClient.GetReservationById(res).StartDate;
            }
            else
            {
            }

        }

        private void btnVerwijder_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            if (myClient.DeleteReservation(lstReservations.SelectedItem.ToString()))
            {
                MessageBox.Show("Reservatie is verwijderd.");

                //Reservation lijst vernieuwen
                lstReservations.Items.Clear();
                Reservation reservation = new Reservation();
                List<Reservation> lijstReservation = myClient.GetAllReservations();
                lijstReservation.Add(reservation);
                foreach (Reservation r in lijstReservation)
                {
                    lstReservations.Items.Add(r.Id);
                }

            }
            else
            {
                MessageBox.Show("Reservatie verwijderen is mislukt.");
            }
        }

        private void btnVerwijderen_Click(object sender, RoutedEventArgs e)
        {
            ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
            string contractID = lstBedrijvenStopzetten.SelectedItem.ToString();
            Contract contract = new Contract();
            int x = myClient.DeleteContract(contractID);
            if (x > 0)
            {
                lstBedrijvenStopzetten.Items.Clear();
                ServiceReference1.Service1Client myClient2 = new ServiceReference1.Service1Client();
                Contract contract2 = new Contract();
                List<Contract> LijstCompaniesStop = myClient2.GetAllContracts();
                LijstCompaniesStop.Add(contract2);
                foreach (Contract r in LijstCompaniesStop)
                {
                    lstBedrijvenStopzetten.Items.Add(r.Id);
                }
                MessageBox.Show("Contract verwijdert!");
            }
        }

        private void lstBedrijvenStopzetten_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstBedrijvenStopzetten.Items.Count > 0)
            {
                ServiceReference1.Service1Client myClient = new ServiceReference1.Service1Client();
                string contractID = lstBedrijvenStopzetten.SelectedItem.ToString();
                Company company = new Company();
                ContractFormula formula = new ContractFormula();
                Contract contract = new Contract();

                contract = myClient.GetContractByCompany(contractID);
                txtStartDatumStop.Text = contract.StartDate.ToString();
                txtEindDatumStop.Text = contract.EndDate.ToString();

                company = myClient.GetContractByCompanyWithName(contractID);
                txtBedrijfsnaam.Text = company.Name;

                formula = myClient.GetContractByCompanyWithDescription(contractID);
                txtFormulaStop.Text = formula.Description;
            }
        }


    }
}
